#include <array.h>
Inheritance diagram for mudbox::Store< type >:
This class represents a simple array and should be used in all circumstances instead of plain C-style arrays. The class is templated, so that you can use to instantiate arrays of any type.
Its advantages of Store arrays include:
Typically, you create an empty Store instance, and then set the number of items (if you know it) by calling SetItemCount(). Alternatively, you can add elements into the array one by one, using the Add() function. If the elements of the array are class instances, their constructors and destructors can optionally be called when the array space is allocated and deallocated. (You must call the right methods.) For this reason, any class from which you will create an array should have a constructor that takes no arguments.
When the array is extended (for example when new elements are added by calling the Add() function), the existing array elements will be copied to the new location with the "=" operator of the element class. For this reason, you should ensure that this operator will work correctly, especially in cases where your class contains pointers. (If you have no "-" operator defined in the data class, it will just do a direct memory copy.)
A Store instance sometimes has more memory allocated than needed by the number of elements in the array. See the Optimize() function for more details.
Note: If you use the "=" operator to copy one entire Store array to another, the original array is left empty. (This is a speed optimization.) If you want to do a deep copy of the contents of the Store array, you should instead use the Clone() function as follows:
Store<int> a; a = b.Clone();
Public Member Functions |
|
Store (const char *sName="unknown") | |
Creates an empty array. |
|
Store (unsigned int iSize, const char *sName) | |
Creates an array with space for some
elements already allocated. |
|
Store (const type *pArray, int iSize, const char *sName="unknown") | |
Creates an array and populates it will
information copied from another array. |
|
Store (bool bNoObjects, const char *sName) | |
Creates an empty array, allowing you to
specify if elements have constructors or destructors that need to
be called. |
|
Store (const Store &s) | |
Creates an array from another one. The other
array becomes empty after the operation. |
|
~Store (void) | |
Destroys the contents of the array and
deallocate its memory. |
|
bool | Copy (Store &s) const |
Copies the contents of another array into
this one, duplicating all data. Returns true if successful.
|
|
Store | Clone (void) const |
Returns a copy of this array. The returned
copy will have a duplicate of all data in the original. |
|
void | Clone (Store &s) const |
Copies the contents of another array into
this one, duplicating all data. |
|
void | Clear (bool bDestruct=false) |
Clears the array and deallocates its memory.
|
|
bool | SetItemCount (unsigned int iSize, bool bKeepContent=false) |
Sets the logical size of the array. Returns
true if successful. |
|
bool | Allocate (unsigned int iSize, bool bKeepContent=false) |
Preallocates memory for the an array,
without changing the array's logical size. Returns true if
successful. |
|
bool | Extend (unsigned int iIndex) |
Extends the logical size of the array.
Returns true if successful. |
|
bool | Extend (int iIndex) |
Extends the logical size of the array.
Returns true if successful. |
|
void | RemoveTail (int iItemCount=1) |
Removes the final items from the array.
|
|
void | Fill (type cPattern) |
Fills the array with a specified element.
|
|
unsigned int | Add (const type &e) |
Adds a new item to the array, increasing the
array size by 1. The index of the added element is returned.
|
|
unsigned int | Add (type &e) |
Adds a new item to the array, increasing the
array size by 1. The index of the added element is returned.
|
|
Store & | operator= (const Store &s) |
Transfers the contents of another array to
this one, leaving the original array empty. |
|
void | GetFrom (Store &s) |
Transfers the contents of another array to
this one, leaving the original array empty. |
|
const type & | operator[] (unsigned int iIndex) const |
Returns an indexed item from the array.
|
|
type & | operator[] (unsigned int iIndex) |
Returns an indexed item from the array.
|
|
const type & | operator[] (int iIndex) const |
Returns an indexed item from the array.
|
|
type & | operator[] (int iIndex) |
Returns an indexed item from the array.
|
|
type * | operator+ (unsigned int iIndex) |
Returns a pointer to an item in the array.
THIS FUNCTION IS UNSAFE. |
|
type * | operator+ (int iIndex) |
Returns a pointer to an item in the array.
THIS FUNCTION IS UNSAFE. |
|
bool | IsEmpty (void) const |
Returns true if the array has no
items. |
|
operator bool (void) const | |
Allows you to use the array as a boolean in
if statements. Evaluates to true if the array is not
empty. |
|
bool | operator! (void) const |
Evaluates to true if the array
contains no items. |
|
void | SetBuffer (type *pData, unsigned int iSize=0) |
Makes a Store array from a regular C or
C++ array. |
|
void | Serialize (class Stream &s) |
Serializes the the array and its contents
from/to a stream. |
|
void | Sort (void) |
Sorts the items in the array. |
|
type * | Find (type a) |
Returns a pointer to the item in the array
with a particular value. The array must be sorted before using this
call. |
|
type * | Find (type *a) |
Returns a pointer to the item in the array
with a particular value. The array must be sorted before using this
call. |
|
unsigned int | Remove (const type &e) |
Remove every occurrence of a specified item
from the array. Returns the number of removed items. |
|
void | Remove (unsigned int iIndex) |
Removes the item at the specified index from
the array. |
|
unsigned int | RemoveDuplicates (void) |
Removes duplicated items from the array
after sorting it. |
|
unsigned int | ItemCount (void) const |
Returns the number of items in the array.
|
|
type & | Last (void) |
Returns the last item in the array, or 0 if
the array is empty. |
|
void | Optimize (void) |
Reduces the allocated memory size to match
the size of the current elements in the array. |
|
Public Attributes |
|
unsigned int | m_iSize |
Static Protected Member Functions |
|
int | Compare (const void *a, const void *b) |
|
Creates an empty array.
00307 : Array<type>( sName ) { m_iSize = 0; }; |
|
Creates an array with space for some elements already allocated. The content of the array is undefined. (That is, constructors are not called. To create an array where constructors and destructors are called automatically, use this call:
Store<myType> myInstance(false);
00320 : Array<type>( sName, iSize ) { m_iSize = iSize; }; |
|
Creates an array and populates it will information copied from another array. Note that the information from the source array is copied over using memcpy. It is the caller's responsibility to check that the array being copied from contains enough values to copy before calling this constructor.
00331 : Array<type>( sName, pArray, iSize ) { m_iSize = iSize; }; |
|
Creates an empty array, allowing you to specify if elements have constructors or destructors that need to be called.
00337 : Array<type>( sName, bNoObjects ) { m_iSize = 0; }; |
|
Creates an array from another one. The other array becomes empty after the operation.
00342 : Array<type>( s.m_sName, s ) { m_iSize = s.m_iSize; s.m_iSize = 0; }; |
|
Destroys the contents of the array and deallocate its memory. 00345 { Clear( !Array<type>::m_bData ); }; |
|
Copies the contents of another array into this one, duplicating all data. Returns true if successful.
00350 { s.m_iSize = m_iSize; return Array<type>::Copy( s ); };
|
|
Returns a copy of this array. The returned copy will have a duplicate of all data in the original. Reimplemented from mudbox::Array< type >. |
|
Copies the contents of another array into this one, duplicating all data.
00358 { s.SetItemCount( m_iSize ); Copy( s ); }; |
|
Clears the array and deallocates its memory.
Reimplemented from mudbox::Array< type >. 00363 { Array<type>::Clear( bDestruct ); m_iSize = 0; }; |
|
Sets the logical size of the array. Returns true if successful. If the new size is smaller than the current size, items at the end of the array will be discarded. If the new size is larger, then a new memory block will be allocated. In this case, the existing items will only be copied to the new block only if bKeepContent is set to true.
|
|
Preallocates memory for the an array, without changing the array's logical size. Returns true if successful. This method can be used to increase efficiency when an array is expected to grow by a large number of small steps. For example, if you expect to add 50 items to an array one at a time, you can call Allocate() first to allocate the required space. This will prevent the need for frequent reallocation.
00398 { if ( !bKeepContent ) Clear(); return Array<type>::Alloc( iSize ); }; |
|
Extends the logical size of the array. Returns true if successful. If the specified size is not larger than the current array, nothing will happen. Otherwise the logical size of the array will be extended to one more than the specified index. New memory will be allocated as necessary to hold the new items. Returns true if successful.
Reimplemented from mudbox::Array< type >. |
|
Extends the logical size of the array. Returns true if successful. If the specified size is not larger than the current array, nothing will happen. Otherwise the logical size of the array will be extended to one more than the specified index. New memory will be allocated as necessary to hold the new items. Returns true if successful.
00424 { return Extend( (unsigned int)iIndex ); }; |
|
Removes the final items from the array.
00429 { SetItemCount( ItemCount()-iItemCount ); }; |
|
Fills the array with a specified element.
00434 { for ( unsigned int i = 0; i < m_iSize; i++ ) operator[](i) = cPattern; }; |
|
Adds a new item to the array, increasing the array size by 1. The index of the added element is returned. If you plan to add a number of objects using this method, you can improve efficiency by preallocating all the space you will need using the Allocate() method.
00442 { if( Array<type>::Extend( m_iSize ) ) { Array<type>::operator[]( m_iSize ) = e; return m_iSize++; } else return 0xffffffff; }; |
|
Adds a new item to the array, increasing the array size by 1. The index of the added element is returned. If you plan to add a number of objects using this method, you can improve efficiency by preallocating all the space you will need using the Allocate() method.
00450 { if( Array<type>::Extend( m_iSize ) ) { Array<type>::operator[]( m_iSize ) = e; return m_iSize++; } else return 0xffffffff; }; |
|
Transfers the contents of another array to this one, leaving the original array empty. If you want to copy the contents of the source array (instead of transferring it), use the Copy() or Clone() functions. 00455 { m_iSize = s.m_iSize; s.m_iSize = 0; Array<type>::operator =( s ); return *this; }; |
|
Transfers the contents of another array to this one, leaving the original array empty. If you want to copy the contents of the source array (instead of transferring it), use the Copy() or Clone() functions.
00462 { m_iSize = s.m_iSize; Array<type>::operator =( s ); }; |
|
Returns an indexed item from the array. Reimplemented from mudbox::Array< type >.
00465 { return Array<type>::m_pArray[iIndex]; };
|
|
Returns an indexed item from the array. Reimplemented from mudbox::Array< type >. 00468 { MB_ASSERT( iIndex < m_iSize ); return Array<type>::m_pArray[iIndex]; }; |
|
Returns an indexed item from the array. 00471 { return operator[]( (unsigned int)iIndex ); }; |
|
Returns an indexed item from the array. Reimplemented from mudbox::Array< type >. 00474 { return operator[]( (unsigned int)iIndex ); }; |
|
Returns a pointer to an item in the array. THIS FUNCTION IS UNSAFE. This method allows you to access the address of an array element. However, since many basic array operations can change the memory location of items in the array, this pointer should only be referenced immediately, and never kept around. Reimplemented from mudbox::Array< type >. 00481 { return &operator[]( iIndex ); }; |
|
Returns a pointer to an item in the array. THIS FUNCTION IS UNSAFE. This method allows you to access the address of an array element. However, since many basic array operations can change the memory location of items in the array, this pointer should only be referenced immediately, and never kept around. Reimplemented from mudbox::Array< type >. 00488 { return &operator[]( iIndex ); }; |
|
Returns true if the array has no items. 00491 { return ItemCount() == 0; }; |
|
Allows you to use the array as a boolean in if statements. Evaluates to true if the array is not empty. 00494 { return !IsEmpty(); }; |
|
Evaluates to true if the array contains no items. 00497 { return IsEmpty(); }; |
|
Makes a Store array from a regular C or C++ array. This method takes a pointer to a block of memory that contains a C-style array of elements of the appropriate type, and makes that into a Mudbox array object. Once this is called, the Store item will control the memory block, and extend or deallocate it as needed, as part of its own methods and destructor. It is the responsibility of the caller to ensure that the source array is at least as big as the specified size. 00508 { m_iSize = Array<type>::m_iSize = iSize; Array<type>::m_pArray = pData; }; |
|
Serializes the the array and its contents from/to a stream.
00290 { 00291 if ( !s.IsStoring() ) 00292 SetItemCount( s.ReadInt() ); 00293 else 00294 s << m_iSize; 00295 for ( unsigned int i = 0; i < m_iSize; i++ ) 00296 s == operator[]( i ); 00297 }; |
|
Sorts the items in the array. This method will sort all the items in the array from least to greatest. If the array is of a non-numeric or non-string type, this method determines the relative ordering of items using the subtraction ("-") operator. In order to use this method, you must ensure that the subtraction operator is defined for the data type stored in this array, with these properties: if A is less than B, then A-B must return a negative integer if A is greater than B, then A-B must return a positive integer if A is equivalent to B, then A-B must return 0 00527 { QuickSort( Array<type>::m_pArray, m_iSize, sizeof(type), Compare ); }; |
|
Returns a pointer to the item in the array with a particular value. The array must be sorted before using this call. This method uses a binary search to return a pointer to an element in the array with the specified value. If the value is not found, then NULL is returned. Before this method can be called, the array must be sorted (by calling the Sort() method). Note: The pointer returned by this function should not be kept for later use, as the contents of an array can move around in memory as items are added and removed. It should only be used immediately.
00539 { return (type *) bsearch( &a, Array<type>::m_pArray, m_iSize, sizeof(type), Compare ); }; |
|
Returns a pointer to the item in the array with a particular value. The array must be sorted before using this call. This method uses a binary search to return a pointer to an element in the array with the specified value. If the value is not found, then NULL is returned. Before this method can be called, the array must be sorted (by calling the Sort() method). Note: The pointer returned by this function should not be kept for later use, as the contents of an array can move around in memory as items are added and removed. It should only be used immediately. 00549 { return (type *) bsearch( a, Array<type>::m_pArray, m_iSize, sizeof(type), Compare ); }; |
|
Remove every occurrence of a specified item from the array. Returns the number of removed items.
00555 { 00556 unsigned int j = 0, i = 0; 00557 while ( i < ItemCount() ) 00558 { 00559 if ( operator[]( i ) != e ) 00560 operator[]( j++ ) = operator[]( i ); 00561 i++; 00562 }; 00563 SetItemCount( j ); 00564 return i-j; 00565 }; |
|
Removes the item at the specified index from the array.
00571 { 00572 while ( iIndex+1 < ItemCount( ) ) 00573 { 00574 operator[]( iIndex ) = operator[]( iIndex+1 ); 00575 iIndex++; 00576 }; 00577 SetItemCount( Min(iIndex,ItemCount()-1) ); 00578 }; |
|
Removes duplicated items from the array after sorting it. See the Sort() method for information on what makes an array Sort-able. This method sorts the contents of the array, then goes through and removes any duplicate entries, leaving a list of unique items. It returns the number of items that were removed. 00587 { 00588 if ( ItemCount() == 0 ) 00589 return 0; 00590 Sort(); 00591 unsigned int j = 0; 00592 for ( unsigned int i = 0; i < ItemCount()-1; i++ ) 00593 { 00594 if ( operator[]( i ) != operator[]( i+1 ) ) 00595 operator[]( j++ ) = operator[]( i ); 00596 }; 00597 operator[]( j++ ) = operator[]( ItemCount()-1 ); 00598 unsigned iRemoved = ItemCount()-j; 00599 m_iSize = j; 00600 return iRemoved; 00601 }; |
|
Returns the number of items in the array.
00604 { return m_iSize; };
|
|
Returns the last item in the array, or 0 if the array is empty. 00607 { if ( ItemCount() ) return operator []( ItemCount()-1 ); else throw new Error( "Store::LastItem called for an empty store" ); }; |
|
Reduces the allocated memory size to match the size of the current elements in the array. Arrays derived from Store automatically grow their memory as needed to accommodate the items that are added to them. For efficiency, when an array expands it allocates more memory than it immediately needs. This reduces the number of expensive memory allocations. The Optimize methods allocates a new block of memory, precisely the size needed to hold the current contents of the array, then copies the array data to this new block before deleting the existing block. Use it to minimize the memory footprint of arrays that are unlikely to expand, but that you need to keep around. 00618 { Array<type>::Alloc( m_iSize ); }; |
|
00621 { return (int)(*((type *)a)-*((type *)b)); };
|
|
Reimplemented from mudbox::Array< type >. |